/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bm.util;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mirza at Bimasakti
 */
public class MrResultSetAdapter implements java.io.Serializable{

    private ResultSet rs = null;
    private ArrayList data = null;
    private int index = 0;
    private int rowCount = 0;
    
    public MrResultSetAdapter(ResultSet rs) {
        try {
            data = new ArrayList();
            ResultSetMetaData rsmd = rs.getMetaData();
            int numColumns = rsmd.getColumnCount();            
            while (rs.next()) {
                HashMap h = new HashMap();
                for(int i = 1; i <= numColumns; i++){
                    String colomnName = rsmd.getColumnName(i);
                    h.put(colomnName, rs.getObject(i));
                }
                data.add(rowCount, h);
                rowCount++;
            }
        } catch (SQLException ex) {
            Logger.getLogger(MrResultSetAdapter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean next() {
        if (index < rowCount){
            index++;
            return true;
        } else {
            return false;
        }
    }

    public int getInt(String fieldName) {
        HashMap map = (HashMap) data.get(index-1);
        return (Integer) map.get(fieldName);
    }

    public String getString(String fieldName) {
        HashMap map = (HashMap) data.get(index-1);
        return (String) map.get(fieldName);
    }
    
    public Date getDate(String fieldName){
        HashMap map = (HashMap) data.get(index-1);
        return (Date) map.get(fieldName);
    }
}
